package styx.lex;


import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class CuttingView extends SurfaceView implements SurfaceHolder.Callback {

	
	private int mCanvasHeight = 1;
	private int mCanvasWidth = 1;
	
	private Context mContext;
	private Bitmap mBackgroundImage;
	private SurfaceHolder mSurfaceHolder;
	private Handler mHandler;
	
	private boolean mRunning;

	class CuttingThread extends Thread {
        
		public CuttingThread (SurfaceHolder surfaceHolder, Context context,
                Handler handler) {
			mSurfaceHolder = surfaceHolder;
			mHandler = handler;
			mContext = context;
			
		}
		
		public void setRunning(boolean b) {
			mRunning = b;
		}

		public void setSurfaceSize(int width, int height) {
			// TODO Auto-generated method stub
			
		}

		public boolean doKeyUp(int keyCode, KeyEvent msg) {
			return false;
		}

		public boolean doKeyDown(int keyCode, KeyEvent msg) {
			// TODO Auto-generated method stub
			return false;
		}

		public void pause() {
			// TODO Auto-generated method stub
			
		}

		public boolean doTouchEvent(MotionEvent event) {
			// TODO Auto-generated method stub
			return false;
		}

		public void restoreState(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			
		}

		public void setState(int state) {
			
		}

		public void saveState(Bundle outState) {
			// TODO Auto-generated method stub
			
		}
		
		public void run()
		{
			
		}

	}

	private CuttingThread thread;
	
	public CuttingView(Context context) {
		super(context);

		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		
		thread = new CuttingThread(holder, context, new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
			}
		});
		
		setFocusable(true);
	}
	
	public CuttingThread getThread() {
		return thread;
	}
	
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {
        return thread.doKeyDown(keyCode, msg);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent msg) {
        return thread.doKeyUp(keyCode, msg);
    }
    
    @Override
	public boolean onTouchEvent(MotionEvent event) {
        return thread.doTouchEvent(event);
    }
    

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        if (!hasWindowFocus) thread.pause();
    }

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
        thread.setSurfaceSize(width, height);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		thread.setRunning(true);
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		thread.setRunning(false);
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}
	}
	
}
